package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.cons.FileNameCode;
import cn.ztydata.teachingsystem.heaven.entity.Attachment;
import cn.ztydata.teachingsystem.heaven.service.ResourceService;
import cn.ztydata.teachingsystem.heaven.util.FileConverter;
import cn.ztydata.teachingsystem.heaven.web.dto.AttachmentDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;

/**
 * 附件管理控制器类
 *
 * @author fanruiping
 * @since 2014-12-22
 */
@RequestMapping("/attachment")
@Controller
public class AttachmentController extends BaseController {
    @Autowired
    private ResourceService resourceService;

    @Autowired
    private FileConverter fileConverter;

    /**
     * 显示资源附件
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2014-12-22
     */
    @RequestMapping(value = "/show/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult show(@PathVariable Long id){
        Attachment entity = resourceService.getAttachmentDetail(id);

        if(null == entity){
            log.debug("attachment is not exist");

            return new JsonResult(false,"指定资源不存在");
        }

        log.debug("return attachment detail");

        return new JsonResultData<>(mapper.map(entity, AttachmentDto.class));
    }

    /**
     * 资源附件
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2014-12-22
     */
    @RequestMapping(value = "/save", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult save(@RequestBody AttachmentDto dto){
        try{
            resourceService.updateAttachment(mapper.map(dto, Attachment.class));

            if(Arrays.asList(new String[]{"doc","docx","rtf","xls","xlsx","ppt","pptx"}).contains(dto.getSuffix())){
                log.debug("上传文件需要转换");

                 fileConverter.newTask("resource:"+ FileNameCode.resourceMap.get(1) +":"+ dto.getId().toString());

                 log.info("资源："+dto.getName()+"加入转换");
            }
        }catch(IOException ex){
            log.debug("catch IOException");

            log.error("资源："+dto.getName()+"转换任务IO异常");
        }catch(ServiceException ex){
            log.debug("catch ServiceException:return false");

            return new JsonResult(false, ex.getMessage());
        }

        log.debug("return true");

        return new JsonResult(true, "保存附件成功");
    }

    /**
     * 指定资源继续转换
     * @param id 资源编号
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2014-12-25
     */
    @RequestMapping(value = "/converter/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult converter(@PathVariable Long id){
        Attachment detail = resourceService.getAttachmentDetail(id);

        if(null == detail || detail.getDownloadUrl().isEmpty()){
            log.debug("attachment is not exist or download url is empty:return false");

            log.warn("要转换的资源附件不存在:return false");

            return new JsonResult(false,"资源附件不存在");
        }

        try{
            log.debug("资源附件加入转换任务");

            fileConverter.newTask("resource:"+ FileNameCode.resourceMap.get(1) +":"+ String.valueOf(id));

            log.info("文件："+detail.getFileName()+"加入转换");
        }catch (IOException ex){
            log.debug("catch IOException");

            log.error("文件："+detail.getFileName()+"转换IO异常");
        }

        log.debug("return true");

        return new JsonResult(true,"文件正在转换，请刷新查看");
    }
}
